<?php
if (isset($_POST['editer'])) 
{
	$req = $bdd->prepare('SELECT * FROM chapitres WHERE id=:id');
	$req->execute(array('id' => $_POST['id']));
	$donnees = $req->fetch();
	?>
	<h1>Editer ce chapitre</h1>
	<form method="post" action="index.php?page=admin_chapitres">
		<table border="0">
		<tr><td><label for="titre">Titre du livre</label></td>
		<td><input type="text" name="titre" value="<?php echo stripslashes($donnees['titre']); ?>" maxlength="250" size="50"></td></tr>
		<tr><td><label for="categorie">Catégorie</label></td>
		<td><select name="categorie" id="categorie">
		<?php
		$req_cat = $bdd->query('SELECT * FROM categories_textes ORDER BY nom');
		while ($donnees_cat = $req_cat->fetch())
		{
			?>
			<option value="<?php echo $donnees_cat['id']; ?>">
			<?php echo $donnees_cat['nom']; ?>
			</option>
			<?php
		}
		?>
		</select></td></tr>
		<tr><td colspan="2"><label for="description">Description</label></td></tr>
		<tr><td colspan="2"><textarea id="description" name="description" rows="25" cols="80"><?php echo $donnees['description']; ?></textarea></td></tr>
		<tr><td><label for="lien_pdf">Lien PDF</label></td><td><input type="text" name="lien_pdf" value="<?php echo $donnees['lien_pdf']; ?>" maxlength="250" size="50"></td></tr>
		<td><input type="submit" value="Mettre à jour ce livre" name="modifier" class="bouton" /></td><td><input type="hidden" name="id" value="<?php echo $donnees['id']; ?>" /></td></tr>
		</table>
	</form>
	<?php
}
elseif (isset($_POST['modifier'])) 
{
	$req = $bdd->prepare('UPDATE chapitres SET titre = :titre, description = :description, lien_pdf = :lien_pdf WHERE id=:id');
	$req->execute(array(
		'titre' => stripslashes($_POST['titre']),
		'description' => stripslashes($_POST['description']),
		'lien_pdf' => $_POST['lien_pdf'],
		'id' => $_POST['id']
	));
	header('Location: index.php?page=admin_chapitres'); 
}
elseif (isset($_POST['effacer'])) 
{
	$req = $bdd->prepare('DELETE FROM chapitres WHERE id=:id');
	$req->execute(array('id' => $_POST['id']));
	header('Location: index.php?page=admin_chapitres'); 
}
elseif (isset($_POST['ajouter'])) 
{
	$req = $bdd->prepare('INSERT INTO chapitres (titre, categorie) VALUES (:titre, :categorie)');
	$req->execute(array(
		'titre' => stripslashes($_POST['titre']),
		'categorie' => stripslashes($_POST['categorie'])
	));
	header('Location: index.php?page=admin_chapitres'); 
}
else
{
	?>
	<h1 id="titre">Gestion des livres</h1>
	<table border="0" width="100%">
	<?php
	$req = $bdd->query('SELECT * FROM chapitres ORDER BY titre');
	while ($donnees = $req->fetch())
	{
		?>
		<form method="post" action="index.php?page=admin_chapitres">
			<tr>
			<td width="40%"><?php echo stripslashes($donnees['titre']); ?></td>
			<td width="25%">
			<?php
			$req_cat = $bdd->prepare('SELECT * FROM categories_textes WHERE id=:id');
			$req_cat->execute(array('id' => $donnees['categorie']));
			$donnees_cat = $req_cat->fetch();
			$texte = substr($donnees_cat['nom'], 0, 50); 
			echo stripslashes($texte);
			?>
			</td>
			<td width="5%">
			<?php
			if (!empty($donnees['lien_pdf']))
			{	
				?><img src="./pdf/pdf.png" width="20" height="20" /><?php
			}
			?>
			</td>
			<td width="30%"><input type="hidden" name="id" value="<?php echo $donnees['id']; ?>">
			<input type="submit" value="Editer" name="editer" class="bouton" />
			<input type="submit" value="Effacer" name="effacer" class="bouton" /></td>
			</tr>
		</form>
		<?php
	}
	$req->closeCursor();
	?>
	<tr><td colspan="4"><hr /></td></tr>
	<form method="post" action="index.php?page=admin_chapitres">
		<tr>
		<td><input type="text" name="titre" value="" maxlength="250" size="15"></td>
		<td><select name="categorie" id="categorie">
		<?php
		$req_cat = $bdd->query('SELECT * FROM categories_textes ORDER BY nom');
		while ($donnees_cat = $req_cat->fetch())
		{
			?>
			<option value="<?php echo $donnees_cat['id']; ?>">
			<?php echo stripslashes($donnees_cat['nom']); ?>
			</option>
			<?php
		}
		?>
		</select></td>
		<td width="5%"></td>
		<td width="25%"><input type="submit" value="Ajouter un livre" name="ajouter" class="bouton" /></td>
		</tr>
	</form>
	</table>

<?php
}